define([
    "helpers/network",
    "views/modules/popup",
    "views/modules/pager",
], function (Network, PopupView, CommonPager) {
    var windowsId = "window";
    var pagerId = "pager";
    var totalCountsId = "totalCounts";
    var subTotalSummaryId = "subTotalSummaryId";
    var totalSummaryId = "totalSummaryId";
    var pageIndex = 1;

    function getData(amin) {
        var obj = {
            params: {
                audience: ["Admin"],
                admin: "Yes"
            },
            size: 5,
            sort: ["id,desc"]
        }
        Network.ajax("/ContentMessageView.queryAll.query", obj, function (ret) {
            var retData = ret.data.content;
            if (retData) {
                $$("iconId").setValue(retData.length);
                _.forEach(retData, function (each) {
                    $$("listId").add({id: each.id, name: each.title, text: each.content})
                })
            }
        })
    }

    var showWindow = function (title) {
        PopupView(windowsId, title, {
            rows: [
                {
                    view: "scrollview", scroll: "y", paddingY: 5, paddingX: 10,
                    body: {
                        rows: [
                            {
                                view: "datatable", resizeColumn: true, select: "row", autoheight: true,
                                id: "orderDataTableId",
                                data: [],
                                columns: [
                                    {
                                        id: "id", header: "ID", fillspace: true,
                                    },
                                    {
                                        id: "title", header: "标题", fillspace: true,
                                    },
                                    {
                                        id: "content", header: "内容", fillspace: true,
                                    },
                                    {
                                        id: "creationTime", header: "创建时间", fillspace: true,
                                    },
                                ]
                            },
                            {},
                            {
                                cols: [
                                    {gravity: 1, rows: [{}, CommonPager.genPagerUI(pagerId, onWindowReady)]},
                                    {gravity: 1, rows: [{}, CommonPager.genTotalCountsUI(totalCountsId)]},
                                ]
                            }
                        ],
                    }
                },
                {
                    cols: [
                        {},
                        {
                            view: "button", label: "关闭", width: 90,
                            click: function () {
                                closeWindow()
                            }
                        }
                    ]
                }
            ]
        }, 1200, 600, function () {
            onWindowReady(1)
        }, closeWindow);
    };

    var onWindowReady = function (page) {
        var obj = {
            params: {
                audience: ["Admin"]
            },
            size: 20,
            page: page || pageIndex,
            sort: ["creationTime,desc"]
        };
        Network.ajax("/ContentMessageView.queryAll.query", obj, function (ret) {
            $$("orderDataTableId").clearAll();
            $$("orderDataTableId").parse(ret.data.content);
            CommonPager.refreshPager(ret.data.number, ret.data.totalPages);
            $$(totalCountsId).setValue(ret.data.totalElements + "条数据, 共" + ret.data.totalPages + "页");
        });
    }

    var closeWindow = function () {
        if ($$(windowsId)) {
            $$(windowsId).close();
        }
    };

    var read = function (item) {
        var obj = {
            data: {
                ids: [item.id]
            }
        }
        Network.ajax("/ContentMessageTransactor.read.command", obj, function (ret) {
            $$("listId").remove(item.id);
            var num = $$("iconId").getValue();
            console.log(num)
            if(num){
                $$("iconId").setValue(num ? Number(num)-1 : 0);
            }
            webix.message("操作成功")
        });
    }

    return {
        getData: getData,
        $ui: {
            view: "popup",
            id: "messagePopup",
            width: 300,
            padding: 0,
            css: "list_popup",
            body: {
                type: "clean",
                borderless: true,
                rows: [
                    {
                        id: "listId",
                        view: "list",
                        autoheight: true,
                        data: [],
                        type: {
                            height: 45,
                            // template: "<span class='text'>#text#</span><span class='name'>#name#</span>"
                            template: function (obj) {
                                return "<span class='name'>" + obj.name + "</span><span class='text'>" + obj.text + "</span>"
                            }
                        },
                        on: {
                            onItemClick: function (id) {
                                var item = this.getItem(id);
                                read(item);
                            }
                        },
                    },
                    {
                        id: "showAllId", view: "template", css: "show_all", template: "查看所有 <span class='webix_icon fa-angle-double-right'></span>", height: 40,
                        onClick: {
                            "show_all": function () {
                                showWindow("消息通知");
                            },
                        }
                    }
                ]
            }
        }
    };

});